package com.nuotu.datawall.clouddata.layoutData;

import android.content.Context;
import android.widget.GridView;

import com.google.gson.reflect.TypeToken;
import com.nuotu.datawall.clouddata.R;
import com.nuotu.datawall.clouddata.adapter.Grid_jcsj_adapter;
import com.nuotu.datawall.clouddata.been.BasicData;
import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;

import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class BasicDataLayout {
    private static volatile BasicDataLayout instance;
    private static Context mContext;
    private static GridView gv_jcsj;
    private ArrayList<BasicData> jcsjList;
    private static Grid_jcsj_adapter adapter;
    private int tempSize=0;
    public static BasicDataLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (BasicDataLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new BasicDataLayout();
                }
            }
        }
        return  instance;
    }
    public BasicDataLayout init( GridView gridView){
        gv_jcsj=gridView;
        jcsjList =new ArrayList<BasicData>();
        adapter=new Grid_jcsj_adapter(mContext, R.layout.gv_jcsj_item,jcsjList);
        gv_jcsj.setAdapter(adapter);
        return instance;
    }
    public void refreshData(){
        RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_BASICDATA_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                RtInfo rtInfo=JsonUtil.fromJson(result, RtInfo.class);
                if(rtInfo.getRtcode().equals("000")){
                    List<BasicData> llst =JsonUtil.fromArrayJson(JsonUtil.toJson(rtInfo.getData()),new TypeToken<List<BasicData>>() {}.getType());
                    jcsjList.clear();
                    jcsjList.addAll(llst);
                    x.task().autoPost(new Runnable() {
                        @Override
                        public void run() {
                            changeGridView();
                        }
                    });
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
    private void changeGridView() {
        int size = jcsjList.size();
//        if(size ==0 || tempSize==size){
//            adapter.notifyDataSetChanged();
//            return;
//        }else {
//            tempSize=size;
//        }
        // 计算GridView宽度
        int gridviewWidth = DensityUtil.dip2px(  1215);
        // int ioii=gv_jcsj.getMeasuredWidth();
        // item宽度
        gv_jcsj.setColumnWidth((int)((gridviewWidth/size)*0.9));
        // item之间的间隔
        gv_jcsj.setHorizontalSpacing((int)((gridviewWidth/size)*0.1));
        gv_jcsj.setVerticalSpacing(0);
        gv_jcsj.setStretchMode(GridView.NO_STRETCH);
        gv_jcsj.setNumColumns(size);
        adapter.notifyDataSetChanged();
    }
}
